import 'package:boss/models/material.dart' as material;
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusCommodityByFormField/page/materialSetting/provider/material_setting_provider.dart';
import 'package:boss/ui/page/commodity/material/page/materialPicker/material_picker_page.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class MaterialSubtitle extends StatelessWidget {
  final bool canEdit;

  const MaterialSubtitle({this.canEdit});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(
        horizontal: 30.w,
      ),
      child: Consumer<MaterialSettingProvider>(
        builder: (context, materialSettingProvider, _) {
          String title;
          String subTitle;
          if (canEdit) {
            title = '为当前菜品单独加料';
            subTitle = '如果您编辑了分类下的加料配菜，则该分类下所有菜品均延用';
          } else {
            title = '为${materialSettingProvider.menu.menuName}加料';
            subTitle = '您编辑的加料配菜，仅对当前菜品生效';
          }
          return CustomListTitle(
            title: title,
            trailing: LabelButton(
              title: canEdit ? '新增' : '编辑',
              onTap: () async {
                if (canEdit) {
                  materialSettingProvider.addCommodityMaterials();
                } else {
                  var result = await Navigator.pushNamed(context, MaterialPickerPage.pageName,
                      arguments: {
                        'selectedMaterials': materialSettingProvider.menuMaterials.map((item) => material.Material.fromJson(item.toJson())).toList(),
                      });
                  if (result != null) {
                    materialSettingProvider.setMenuMaterial(result);
                  }
                }
              },
            ),
            subTitle: subTitle,
            isHideDivider: true,
          );
        },
      ),
    );
  }
}
